home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / gdb-4.12 / gdb / target.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  30.9 KB  |  1,227 lines

  1. /* Select target systems and architectures at runtime for GDB.
  2.    Copyright 1990, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include <errno.h>
  23. #include <ctype.h>
  24. #include "target.h"
  25. #include "gdbcmd.h"
  26. #include "symtab.h"
  27. #include "inferior.h"
  28. #include "bfd.h"
  29. #include "symfile.h"
  30. #include "objfiles.h"
  31. #include "wait.h"
  32. #include <signal.h>
  33.  
  34. extern int errno;
  35.  
  36. static void
  37. target_info PARAMS ((char *, int));
  38.  
  39. static void
  40. cleanup_target PARAMS ((struct target_ops *));
  41.  
  42. static void
  43. maybe_kill_then_create_inferior PARAMS ((char *, char *, char **));
  44.  
  45. static void
  46. maybe_kill_then_attach PARAMS ((char *, int));
  47.  
  48. static void
  49. kill_or_be_killed PARAMS ((int));
  50.  
  51. static void
  52. default_terminal_info PARAMS ((char *, int));
  53.  
  54. static int
  55. nosymbol PARAMS ((char *, CORE_ADDR *));
  56.  
  57. static void
  58. tcomplain PARAMS ((void));
  59.  
  60. static int
  61. nomemory PARAMS ((CORE_ADDR, char *, int, int));
  62.  
  63. static int
  64. return_zero PARAMS ((void));
  65.  
  66. static void
  67. ignore PARAMS ((void));
  68.  
  69. static void
  70. target_command PARAMS ((char *, int));
  71.  
  72. static struct target_ops *
  73. find_default_run_target PARAMS ((char *));
  74.  
  75. /* Pointer to array of target architecture structures; the size of the
  76.    array; the current index into the array; the allocated size of the 
  77.    array.  */
  78. struct target_ops **target_structs;
  79. unsigned target_struct_size;
  80. unsigned target_struct_index;
  81. unsigned target_struct_allocsize;
  82. #define    DEFAULT_ALLOCSIZE    10
  83.  
  84. /* The initial current target, so that there is always a semi-valid
  85.    current target.  */
  86.  
  87. struct target_ops dummy_target = {"None", "None", "",
  88.     0, 0,         /* open, close */
  89.     find_default_attach, 0,  /* attach, detach */
  90.     0, 0,        /* resume, wait */
  91.     0, 0, 0,        /* registers */
  92.     0, 0,         /* memory */
  93.     0, 0,         /* bkpts */
  94.     0, 0, 0, 0, 0,     /* terminal */
  95.     0, 0,         /* kill, load */
  96.     0,             /* lookup_symbol */
  97.     find_default_create_inferior, /* create_inferior */
  98.     0,            /* mourn_inferior */
  99.     0,            /* can_run */
  100.     0,            /* notice_signals */
  101.     dummy_stratum, 0,    /* stratum, next */
  102.     0, 0, 0, 0, 0,    /* all mem, mem, stack, regs, exec */
  103.     0, 0,        /* section pointers */
  104.     OPS_MAGIC,
  105. };
  106.  
  107. /* The target structure we are currently using to talk to a process
  108.    or file or whatever "inferior" we have.  */
  109.  
  110. struct target_ops *current_target;
  111.  
  112. /* The stack of target structures that have been pushed.  */
  113.  
  114. struct target_ops **current_target_stack;
  115.  
  116. /* Command list for target.  */
  117.  
  118. static struct cmd_list_element *targetlist = NULL;
  119.  
  120. /* The user just typed 'target' without the name of a target.  */
  121.  
  122. /* ARGSUSED */
  123. static void
  124. target_command (arg, from_tty)
  125.      char *arg;
  126.      int from_tty;
  127. {
  128.   fputs_filtered ("Argument required (target name).  Try `help target'\n",
  129.           gdb_stdout);
  130. }
  131.  
  132. /* Add a possible target architecture to the list.  */
  133.  
  134. void
  135. add_target (t)
  136.      struct target_ops *t;
  137. {
  138.   if (t->to_magic != OPS_MAGIC)
  139.     {
  140.       fprintf_unfiltered(gdb_stderr, "Magic number of %s target struct wrong\n", 
  141.     t->to_shortname);
  142.       abort();
  143.     }
  144.  
  145.   if (!target_structs)
  146.     {
  147.       target_struct_allocsize = DEFAULT_ALLOCSIZE;
  148.       target_structs = (struct target_ops **) xmalloc
  149.     (target_struct_allocsize * sizeof (*target_structs));
  150.     }
  151.   if (target_struct_size >= target_struct_allocsize)
  152.     {
  153.       target_struct_allocsize *= 2;
  154.       target_structs = (struct target_ops **)
  155.       xrealloc ((char *) target_structs, 
  156.             target_struct_allocsize * sizeof (*target_structs));
  157.     }
  158.   target_structs[target_struct_size++] = t;
  159.   cleanup_target (t);
  160.  
  161.   if (targetlist == NULL)
  162.     add_prefix_cmd ("target", class_run, target_command,
  163.             "Connect to a target machine or process.\n\
  164. The first argument is the type or protocol of the target machine.\n\
  165. Remaining arguments are interpreted by the target protocol.  For more\n\
  166. information on the arguments for a particular protocol, type\n\
  167. `help target ' followed by the protocol name.",
  168.             &targetlist, "target ", 0, &cmdlist);
  169.   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
  170. }
  171.  
  172. /* Stub functions */
  173.  
  174. static void
  175. ignore ()
  176. {
  177. }
  178.  
  179. /* ARGSUSED */
  180. static int
  181. nomemory (memaddr, myaddr, len, write)
  182.      CORE_ADDR memaddr;
  183.      char *myaddr;
  184.      int len;
  185.      int write;
  186. {
  187.   errno = EIO;        /* Can't read/write this location */
  188.   return 0;        /* No bytes handled */
  189. }
  190.  
  191. static void
  192. tcomplain ()
  193. {
  194.   error ("You can't do that when your target is `%s'",
  195.      current_target->to_shortname);
  196. }
  197.  
  198. void
  199. noprocess ()
  200. {
  201.   error ("You can't do that without a process to debug");
  202. }
  203.  
  204. /* ARGSUSED */
  205. static int
  206. nosymbol (name, addrp)
  207.      char *name;
  208.      CORE_ADDR *addrp;
  209. {
  210.   return 1;        /* Symbol does not exist in target env */
  211. }
  212.  
  213. /* ARGSUSED */
  214. static void
  215. default_terminal_info (args, from_tty)
  216.      char *args;
  217.      int from_tty;
  218. {
  219.   printf_unfiltered("No saved terminal information.\n");
  220. }
  221.  
  222. #if 0
  223. /* With strata, this function is no longer needed.  FIXME.  */
  224. /* This is the default target_create_inferior function.  It looks up
  225.    the stack for some target that cares to create inferiors, then
  226.    calls it -- or complains if not found.  */
  227.  
  228. static void
  229. upstack_create_inferior (exec, args, env)
  230.      char *exec;
  231.      char *args;
  232.      char **env;
  233. {
  234.   struct target_ops *t;
  235.  
  236.   for (t = current_target;
  237.        t;
  238.        t = t->to_next)
  239.     {
  240.       if (t->to_create_inferior != upstack_create_inferior)
  241.     {
  242.           t->to_create_inferior (exec, args, env);
  243.       return;
  244.     }
  245.  
  246.     }
  247.   tcomplain();
  248. }
  249. #endif
  250.  
  251. /* This is the default target_create_inferior and target_attach function.
  252.    If the current target is executing, it asks whether to kill it off.
  253.    If this function returns without calling error(), it has killed off
  254.    the target, and the operation should be attempted.  */
  255.  
  256. static void
  257. kill_or_be_killed (from_tty)
  258.      int from_tty;
  259. {
  260.   if (target_has_execution)
  261.     {
  262.       printf_unfiltered ("You are already running a program:\n");
  263.       target_files_info ();
  264.       if (query ("Kill it? ")) {
  265.     target_kill ();
  266.     if (target_has_execution)
  267.       error ("Killing the program did not help.");
  268.     return;
  269.       } else {
  270.     error ("Program not killed.");
  271.       }
  272.     }
  273.   tcomplain();
  274. }
  275.  
  276. static void
  277. maybe_kill_then_attach (args, from_tty)
  278.      char *args;
  279.      int from_tty;
  280. {
  281.   kill_or_be_killed (from_tty);
  282.   target_attach (args, from_tty);
  283. }
  284.  
  285. static void
  286. maybe_kill_then_create_inferior (exec, args, env)
  287.      char *exec;
  288.      char *args;
  289.      char **env;
  290. {
  291.   kill_or_be_killed (0);
  292.   target_create_inferior (exec, args, env);
  293. }
  294.  
  295. /* Clean up a target struct so it no longer has any zero pointers in it.
  296.    We default entries, at least to stubs that print error messages.  */
  297.  
  298. static void
  299. cleanup_target (t)
  300.      struct target_ops *t;
  301. {
  302.  
  303.   /* Check magic number.  If wrong, it probably means someone changed
  304.      the struct definition, but not all the places that initialize one.  */
  305.   if (t->to_magic != OPS_MAGIC)
  306.     {
  307.       fprintf_unfiltered(gdb_stderr, "Magic number of %s target struct wrong\n", 
  308.     t->to_shortname);
  309.       abort();
  310.     }
  311.  
  312. #define de_fault(field, value) \
  313.   if (!t->field)    t->field = value
  314.  
  315.   /*        FIELD            DEFAULT VALUE        */
  316.  
  317.   de_fault (to_open,             (void (*)())tcomplain);
  318.   de_fault (to_close,             (void (*)())ignore);
  319.   de_fault (to_attach,             maybe_kill_then_attach);
  320.   de_fault (to_detach,             (void (*)())ignore);
  321.   de_fault (to_resume,             (void (*)())noprocess);
  322.   de_fault (to_wait,             (int (*)())noprocess);
  323.   de_fault (to_fetch_registers,     (void (*)())ignore);
  324.   de_fault (to_store_registers,        (void (*)())noprocess);
  325.   de_fault (to_prepare_to_store,    (void (*)())noprocess);
  326.   de_fault (to_xfer_memory,        (int (*)())nomemory);
  327.   de_fault (to_files_info,        (void (*)())ignore);
  328.   de_fault (to_insert_breakpoint,    memory_insert_breakpoint);
  329.   de_fault (to_remove_breakpoint,    memory_remove_breakpoint);
  330.   de_fault (to_terminal_init,        ignore);
  331.   de_fault (to_terminal_inferior,    ignore);
  332.   de_fault (to_terminal_ours_for_output,ignore);
  333.   de_fault (to_terminal_ours,        ignore);
  334.   de_fault (to_terminal_info,        default_terminal_info);
  335.   de_fault (to_kill,            (void (*)())noprocess);
  336.   de_fault (to_load,            (void (*)())tcomplain);
  337.   de_fault (to_lookup_symbol,        nosymbol);
  338.   de_fault (to_create_inferior,        maybe_kill_then_create_inferior);
  339.   de_fault (to_mourn_inferior,        (void (*)())noprocess);
  340.   de_fault (to_can_run,            return_zero);
  341.   de_fault (to_notice_signals,        (void (*)())ignore);
  342.   de_fault (to_next,            0);
  343.   de_fault (to_has_all_memory,        0);
  344.   de_fault (to_has_memory,        0);
  345.   de_fault (to_has_stack,        0);
  346.   de_fault (to_has_registers,        0);
  347.   de_fault (to_has_execution,        0);
  348.  
  349. #undef de_fault
  350. }
  351.  
  352. /* Push a new target type into the stack of the existing target accessors,
  353.    possibly superseding some of the existing accessors.
  354.  
  355.    Result is zero if the pushed target ended up on top of the stack,
  356.    nonzero if at least one target is on top of it.
  357.  
  358.    Rather than allow an empty stack, we always have the dummy target at
  359.    the bottom stratum, so we can call the function vectors without
  360.    checking them.  */
  361.  
  362. int
  363. push_target (t)
  364.      struct target_ops *t;
  365. {
  366.   struct target_ops *st, *prev;
  367.  
  368.   for (prev = 0, st = current_target;
  369.        st;
  370.        prev = st, st = st->to_next) {
  371.     if ((int)(t->to_stratum) >= (int)(st->to_stratum))
  372.       break;
  373.   }
  374.  
  375.   while (t->to_stratum == st->to_stratum) {
  376.     /* There's already something on this stratum.  Close it off.  */
  377.     (st->to_close) (0);
  378.     if (prev)
  379.       prev->to_next = st->to_next;    /* Unchain old target_ops */
  380.     else
  381.       current_target = st->to_next;    /* Unchain first on list */
  382.     st = st->to_next;
  383.   }
  384.  
  385.   /* We have removed all targets in our stratum, now add ourself.  */
  386.   t->to_next = st;
  387.   if (prev)
  388.     prev->to_next = t;
  389.   else
  390.     current_target = t;
  391.  
  392.   cleanup_target (current_target);
  393.   return prev != 0;
  394. }
  395.  
  396. /* Remove a target_ops vector from the stack, wherever it may be. 
  397.    Return how many times it was removed (0 or 1 unless bug).  */
  398.  
  399. int
  400. unpush_target (t)
  401.      struct target_ops *t;
  402. {
  403.   struct target_ops *u, *v;
  404.   int result = 0;
  405.  
  406.   for (u = current_target, v = 0;
  407.        u;
  408.        v = u, u = u->to_next)
  409.     if (u == t)
  410.       {
  411.     if (v == 0)
  412.       pop_target();            /* unchain top copy */
  413.     else {
  414.       (t->to_close)(0);        /* Let it clean up */
  415.       v->to_next = t->to_next;    /* unchain middle copy */
  416.     }
  417.     result++;
  418.       }
  419.   return result;
  420. }
  421.  
  422. void
  423. pop_target ()
  424. {
  425.   (current_target->to_close)(0);    /* Let it clean up */
  426.   current_target = current_target->to_next;
  427. #if 0
  428.   /* This will dump core if ever called--push_target expects current_target
  429.      to be non-NULL.  But I don't think it's needed; I don't see how the
  430.      dummy_target could ever be removed from the stack.  */
  431.   if (!current_target)        /* At bottom, push dummy.  */
  432.     push_target (&dummy_target);
  433. #endif
  434. }
  435.  
  436. #undef    MIN
  437. #define MIN(A, B) (((A) <= (B)) ? (A) : (B))
  438.  
  439. /* target_read_string -- read a null terminated string from MEMADDR in target.
  440.    The read may also be terminated early by getting an error from target_xfer_
  441.    memory.
  442.    LEN is the size of the buffer pointed to by MYADDR.  Note that a terminating
  443.    null will only be written if there is sufficient room.  The return value is
  444.    is the number of bytes (including the null) actually transferred.
  445. */
  446.  
  447. int
  448. target_read_string (memaddr, myaddr, len)
  449.      CORE_ADDR memaddr;
  450.      char *myaddr;
  451.      int len;
  452. {
  453.   int tlen, origlen, offset, i;
  454.   char buf[4];
  455.  
  456.   origlen = len;
  457.  
  458.   while (len > 0)
  459.     {
  460.       tlen = MIN (len, 4 - (memaddr & 3));
  461.       offset = memaddr & 3;
  462.  
  463.       if (target_xfer_memory (memaddr & ~3, buf, 4, 0))
  464.     return origlen - len;
  465.  
  466.       for (i = 0; i < tlen; i++)
  467.     {
  468.       *myaddr++ = buf[i + offset];
  469.       if (buf[i + offset] == '\000')
  470.         return (origlen - len) + i + 1;
  471.     }
  472.  
  473.       memaddr += tlen;
  474.       len -= tlen;
  475.     }
  476.   return origlen;
  477. }
  478.  
  479. /* Read LEN bytes of target memory at address MEMADDR, placing the results in
  480.    GDB's memory at MYADDR.  Returns either 0 for success or an errno value
  481.    if any error occurs.
  482.  
  483.    If an error occurs, no guarantee is made about the contents of the data at
  484.    MYADDR.  In particular, the caller should not depend upon partial reads
  485.    filling the buffer with good data.  There is no way for the caller to know
  486.    how much good data might have been transfered anyway.  Callers that can
  487.    deal with partial reads should call target_read_memory_partial. */
  488.  
  489. int
  490. target_read_memory (memaddr, myaddr, len)
  491.      CORE_ADDR memaddr;
  492.      char *myaddr;
  493.      int len;
  494. {
  495.   return target_xfer_memory (memaddr, myaddr, len, 0);
  496. }
  497.  
  498. /* Read LEN bytes of target memory at address MEMADDR, placing the results
  499.    in GDB's memory at MYADDR.  Returns a count of the bytes actually read,
  500.    and optionally an errno value in the location pointed to by ERRNOPTR
  501.    if ERRNOPTR is non-null. */
  502.  
  503. int
  504. target_read_memory_partial (memaddr, myaddr, len, errnoptr)
  505.      CORE_ADDR memaddr;
  506.      char *myaddr;
  507.      int len;
  508.      int *errnoptr;
  509. {
  510.   int nread;    /* Number of bytes actually read. */
  511.   int errcode;    /* Error from last read. */
  512.  
  513.   /* First try a complete read. */
  514.   errcode = target_xfer_memory (memaddr, myaddr, len, 0);
  515.   if (errcode == 0)
  516.     {
  517.       /* Got it all. */
  518.       nread = len;
  519.     }
  520.   else
  521.     {
  522.       /* Loop, reading one byte at a time until we get as much as we can. */
  523.       for (errcode = 0, nread = 0; len > 0 && errcode == 0; nread++, len--)
  524.     {
  525.       errcode = target_xfer_memory (memaddr++, myaddr++, 1, 0);
  526.     }
  527.       /* If an error, the last read was unsuccessful, so adjust count. */
  528.       if (errcode != 0)
  529.     {
  530.       nread--;
  531.     }
  532.     }
  533.   if (errnoptr != NULL)
  534.     {
  535.       *errnoptr = errcode;
  536.     }
  537.   return (nread);
  538. }
  539.  
  540. int
  541. target_write_memory (memaddr, myaddr, len)
  542.      CORE_ADDR memaddr;
  543.      char *myaddr;
  544.      int len;
  545. {
  546.   return target_xfer_memory (memaddr, myaddr, len, 1);
  547. }
  548.  
  549. /* Move memory to or from the targets.  Iterate until all of it has
  550.    been moved, if necessary.  The top target gets priority; anything
  551.    it doesn't want, is offered to the next one down, etc.  Note the
  552.    business with curlen:  if an early target says "no, but I have a
  553.    boundary overlapping this xfer" then we shorten what we offer to
  554.    the subsequent targets so the early guy will get a chance at the
  555.    tail before the subsequent ones do. 
  556.  
  557.    Result is 0 or errno value.  */
  558.  
  559. int
  560. target_xfer_memory (memaddr, myaddr, len, write)
  561.      CORE_ADDR memaddr;
  562.      char *myaddr;
  563.      int len;
  564.      int write;
  565. {
  566.   int curlen;
  567.   int res;
  568.   struct target_ops *t;
  569.  
  570.   /* to_xfer_memory is not guaranteed to set errno, even when it returns
  571.      0.  */
  572.   errno = 0;
  573.  
  574.   /* The quick case is that the top target does it all.  */
  575.   res = current_target->to_xfer_memory
  576.             (memaddr, myaddr, len, write, current_target);
  577.   if (res == len)
  578.     return 0;
  579.  
  580.   if (res > 0)
  581.     goto bump;
  582.   /* If res <= 0 then we call it again in the loop.  Ah well.  */
  583.  
  584.   for (; len > 0;)
  585.     {
  586.       curlen = len;        /* Want to do it all */
  587.       for (t = current_target;
  588.        t;
  589.        t = t->to_has_all_memory? 0: t->to_next)
  590.     {
  591.       res = t->to_xfer_memory(memaddr, myaddr, curlen, write, t);
  592.       if (res > 0) break;    /* Handled all or part of xfer */
  593.       if (res == 0) continue;    /* Handled none */
  594.       curlen = -res;    /* Could handle once we get past res bytes */
  595.     }
  596.       if (res <= 0)
  597.     {
  598.       /* If this address is for nonexistent memory,
  599.          read zeros if reading, or do nothing if writing.  Return error. */
  600.       if (!write)
  601.         memset (myaddr, 0, len);
  602.       if (errno == 0)
  603.         return EIO;
  604.       else
  605.         return errno;
  606.     }
  607. bump:
  608.       memaddr += res;
  609.       myaddr  += res;
  610.       len     -= res;
  611.     }
  612.   return 0;            /* We managed to cover it all somehow. */
  613. }
  614.  
  615.  
  616. /* ARGSUSED */
  617. static void
  618. target_info (args, from_tty)
  619.      char *args;
  620.      int from_tty;
  621. {
  622.   struct target_ops *t;
  623.   int has_all_mem = 0;
  624.   
  625.   if (symfile_objfile != NULL)
  626.     printf_unfiltered ("Symbols from \"%s\".\n", symfile_objfile->name);
  627.  
  628. #ifdef FILES_INFO_HOOK
  629.   if (FILES_INFO_HOOK ())
  630.     return;
  631. #endif
  632.  
  633.   for (t = current_target;
  634.        t;
  635.        t = t->to_next)
  636.     {
  637.       if ((int)(t->to_stratum) <= (int)dummy_stratum)
  638.     continue;
  639.       if (has_all_mem)
  640.     printf_unfiltered("\tWhile running this, gdb does not access memory from...\n");
  641.       printf_unfiltered("%s:\n", t->to_longname);
  642.       (t->to_files_info)(t);
  643.       has_all_mem = t->to_has_all_memory;
  644.     }
  645. }
  646.  
  647. /* This is to be called by the open routine before it does
  648.    anything.  */
  649.  
  650. void
  651. target_preopen (from_tty)
  652.      int from_tty;
  653. {
  654.   dont_repeat();
  655.  
  656.   if (target_has_execution)
  657.     {   
  658.       if (query ("A program is being debugged already.  Kill it? "))
  659.         target_kill ();
  660.       else
  661.         error ("Program not killed.");
  662.     }
  663. }
  664.  
  665. /* Detach a target after doing deferred register stores.  */
  666.  
  667. void
  668. target_detach (args, from_tty)
  669.      char *args;
  670.      int from_tty;
  671. {
  672.   /* Handle any optimized stores to the inferior.  */
  673. #ifdef DO_DEFERRED_STORES
  674.   DO_DEFERRED_STORES;
  675. #endif
  676.   (current_target->to_detach) (args, from_tty);
  677. }
  678.  
  679. /* Look through the list of possible targets for a target that can
  680.    execute a run or attach command without any other data.  This is
  681.    used to locate the default process stratum.
  682.  
  683.    Result is always valid (error() is called for errors).  */
  684.  
  685. static struct target_ops *
  686. find_default_run_target (do_mesg)
  687.      char *do_mesg;
  688. {
  689.   struct target_ops **t;
  690.   struct target_ops *runable = NULL;
  691.   int count;
  692.  
  693.   count = 0;
  694.  
  695.   for (t = target_structs; t < target_structs + target_struct_size;
  696.        ++t)
  697.     {
  698.       if (target_can_run(*t))
  699.     {
  700.       runable = *t;
  701.       ++count;
  702.     }
  703.     }
  704.  
  705.   if (count != 1)
  706.     error ("Don't know how to %s.  Try \"help target\".", do_mesg);
  707.  
  708.   return runable;
  709. }
  710.  
  711. void
  712. find_default_attach (args, from_tty)
  713.      char *args;
  714.      int from_tty;
  715. {
  716.   struct target_ops *t;
  717.  
  718.   t = find_default_run_target("attach");
  719.   (t->to_attach) (args, from_tty);
  720.   return;
  721. }
  722.  
  723. void
  724. find_default_create_inferior (exec_file, allargs, env)
  725.      char *exec_file;
  726.      char *allargs;
  727.      char **env;
  728. {
  729.   struct target_ops *t;
  730.  
  731.   t = find_default_run_target("run");
  732.   (t->to_create_inferior) (exec_file, allargs, env);
  733.   return;
  734. }
  735.  
  736. static int
  737. return_zero ()
  738. {
  739.   return 0;
  740. }
  741.  
  742. struct target_ops *
  743. find_core_target ()
  744. {
  745.   struct target_ops **t;
  746.   struct target_ops *runable = NULL;
  747.   int count;
  748.   
  749.   count = 0;
  750.   
  751.   for (t = target_structs; t < target_structs + target_struct_size;
  752.        ++t)
  753.     {
  754.       if ((*t)->to_stratum == core_stratum)
  755.     {
  756.       runable = *t;
  757.       ++count;
  758.     }
  759.     }
  760.   
  761.   return(count == 1 ? runable : NULL);
  762. }
  763.  
  764. /* This table must match in order and size the signals in enum target_signal
  765.    in target.h.  */
  766. static struct {
  767.   char *name;
  768.   char *string;
  769.   } signals [] =
  770. {
  771.   {"0", "Signal 0"},
  772.   {"SIGHUP", "Hangup"},
  773.   {"SIGINT", "Interrupt"},
  774.   {"SIGQUIT", "Quit"},
  775.   {"SIGILL", "Illegal instruction"},
  776.   {"SIGTRAP", "Trace/breakpoint trap"},
  777.   {"SIGABRT", "Aborted"},
  778.   {"SIGEMT", "Emulation trap"},
  779.   {"SIGFPE", "Arithmetic exception"},
  780.   {"SIGKILL", "Killed"},
  781.   {"SIGBUS", "Bus error"},
  782.   {"SIGSEGV", "Segmentation fault"},
  783.   {"SIGSYS", "Bad system call"},
  784.   {"SIGPIPE", "Broken pipe"},
  785.   {"SIGALRM", "Alarm clock"},
  786.   {"SIGTERM", "Terminated"},
  787.   {"SIGURG", "Urgent I/O condition"},
  788.   {"SIGSTOP", "Stopped (signal)"},
  789.   {"SIGTSTP", "Stopped (user)"},
  790.   {"SIGCONT", "Continued"},
  791.   {"SIGCHLD", "Child status changed"},
  792.   {"SIGTTIN", "Stopped (tty input)"},
  793.   {"SIGTTOU", "Stopped (tty output)"},
  794.   {"SIGIO", "I/O possible"},
  795.   {"SIGXCPU", "CPU time limit exceeded"},
  796.   {"SIGXFSZ", "File size limit exceeded"},
  797.   {"SIGVTALRM", "Virtual timer expired"},
  798.   {"SIGPROF", "Profiling timer expired"},
  799.   {"SIGWINCH", "Window size changed"},
  800.   {"SIGLOST", "Resource lost"},
  801.   {"SIGUSR1", "User defined signal 1"},
  802.   {"SIGUSR2", "User defined signal 2"},
  803.   {"SIGPWR", "Power fail/restart"},
  804.   {"SIGPOLL", "Pollable event occurred"},
  805.   {"SIGWIND", "SIGWIND"},
  806.   {"SIGPHONE", "SIGPHONE"},
  807.   {"SIGWAITING", "Process's LWPs are blocked"},
  808.   {"SIGLWP", "Signal LWP"},
  809.   {"SIGDANGER", "Swap space dangerously low"},
  810.   {"SIGGRANT", "Monitor mode granted"},
  811.   {"SIGRETRACT", "Need to relinguish monitor mode"},
  812.   {"SIGMSG", "Monitor mode data available"},
  813.   {"SIGSOUND", "Sound completed"},
  814.   {"SIGSAK", "Secure attention"},
  815.   {NULL, "Unknown signal"},
  816.   {NULL, "Internal error: printing TARGET_SIGNAL_DEFAULT"},
  817.  
  818.   /* Last entry, used to check whether the table is the right size.  */
  819.   {NULL, "TARGET_SIGNAL_MAGIC"}
  820. };
  821.  
  822. /* Return the string for a signal.  */
  823. char *
  824. target_signal_to_string (sig)
  825.      enum target_signal sig;
  826. {
  827.   return signals[sig].string;
  828. }
  829.  
  830. /* Return the name for a signal.  */
  831. char *
  832. target_signal_to_name (sig)
  833.      enum target_signal sig;
  834. {
  835.   if (sig == TARGET_SIGNAL_UNKNOWN)
  836.     /* I think the code which prints this will always print it along with
  837.        the string, so no need to be verbose.  */
  838.     return "?";
  839.   return signals[sig].name;
  840. }
  841.  
  842. /* Given a name, return its signal.  */
  843. enum target_signal
  844. target_signal_from_name (name)
  845.      char *name;
  846. {
  847.   enum target_signal sig;
  848.  
  849.   /* It's possible we also should allow "SIGCLD" as well as "SIGCHLD"
  850.      for TARGET_SIGNAL_SIGCHLD.  SIGIOT, on the other hand, is more
  851.      questionable; seems like by now people should call it SIGABRT
  852.      instead.  */
  853.  
  854.   /* This ugly cast brought to you by the native VAX compiler.  */
  855.   for (sig = TARGET_SIGNAL_HUP;
  856.        signals[sig].name != NULL;
  857.        sig = (enum target_signal)((int)sig + 1))
  858.     if (STREQ (name, signals[sig].name))
  859.       return sig;
  860.   return TARGET_SIGNAL_UNKNOWN;
  861. }
  862.  
  863. /* The following functions are to help certain targets deal
  864.    with the signal/waitstatus stuff.  They could just as well be in
  865.    a file called native-utils.c or unixwaitstatus-utils.c or whatever.  */
  866.  
  867. /* Convert host signal to our signals.  */
  868. enum target_signal
  869. target_signal_from_host (hostsig)
  870.      int hostsig;
  871. {
  872.   /* A switch statement would make sense but would require special kludges
  873.      to deal with the cases where more than one signal has the same number.  */
  874.  
  875.   if (hostsig == 0) return TARGET_SIGNAL_0;
  876.  
  877. #if defined (SIGHUP)
  878.   if (hostsig == SIGHUP) return TARGET_SIGNAL_HUP;
  879. #endif
  880. #if defined (SIGINT)
  881.   if (hostsig == SIGINT) return TARGET_SIGNAL_INT;
  882. #endif
  883. #if defined (SIGQUIT)
  884.   if (hostsig == SIGQUIT) return TARGET_SIGNAL_QUIT;
  885. #endif
  886. #if defined (SIGILL)
  887.   if (hostsig == SIGILL) return TARGET_SIGNAL_ILL;
  888. #endif
  889. #if defined (SIGTRAP)
  890.   if (hostsig == SIGTRAP) return TARGET_SIGNAL_TRAP;
  891. #endif
  892. #if defined (SIGABRT)
  893.   if (hostsig == SIGABRT) return TARGET_SIGNAL_ABRT;
  894. #endif
  895. #if defined (SIGEMT)
  896.   if (hostsig == SIGEMT) return TARGET_SIGNAL_EMT;
  897. #endif
  898. #if defined (SIGFPE)
  899.   if (hostsig == SIGFPE) return TARGET_SIGNAL_FPE;
  900. #endif
  901. #if defined (SIGKILL)
  902.   if (hostsig == SIGKILL) return TARGET_SIGNAL_KILL;
  903. #endif
  904. #if defined (SIGBUS)
  905.   if (hostsig == SIGBUS) return TARGET_SIGNAL_BUS;
  906. #endif
  907. #if defined (SIGSEGV)
  908.   if (hostsig == SIGSEGV) return TARGET_SIGNAL_SEGV;
  909. #endif
  910. #if defined (SIGSYS)
  911.   if (hostsig == SIGSYS) return TARGET_SIGNAL_SYS;
  912. #endif
  913. #if defined (SIGPIPE)
  914.   if (hostsig == SIGPIPE) return TARGET_SIGNAL_PIPE;
  915. #endif
  916. #if defined (SIGALRM)
  917.   if (hostsig == SIGALRM) return TARGET_SIGNAL_ALRM;
  918. #endif
  919. #if defined (SIGTERM)
  920.   if (hostsig == SIGTERM) return TARGET_SIGNAL_TERM;
  921. #endif
  922. #if defined (SIGUSR1)
  923.   if (hostsig == SIGUSR1) return TARGET_SIGNAL_USR1;
  924. #endif
  925. #if defined (SIGUSR2)
  926.   if (hostsig == SIGUSR2) return TARGET_SIGNAL_USR2;
  927. #endif
  928. #if defined (SIGCLD)
  929.   if (hostsig == SIGCLD) return TARGET_SIGNAL_CHLD;
  930. #endif
  931. #if defined (SIGCHLD)
  932.   if (hostsig == SIGCHLD) return TARGET_SIGNAL_CHLD;
  933. #endif
  934. #if defined (SIGPWR)
  935.   if (hostsig == SIGPWR) return TARGET_SIGNAL_PWR;
  936. #endif
  937. #if defined (SIGWINCH)
  938.   if (hostsig == SIGWINCH) return TARGET_SIGNAL_WINCH;
  939. #endif
  940. #if defined (SIGURG)
  941.   if (hostsig == SIGURG) return TARGET_SIGNAL_URG;
  942. #endif
  943. #if defined (SIGIO)
  944.   if (hostsig == SIGIO) return TARGET_SIGNAL_IO;
  945. #endif
  946. #if defined (SIGPOLL)
  947.   if (hostsig == SIGPOLL) return TARGET_SIGNAL_POLL;
  948. #endif
  949. #if defined (SIGSTOP)
  950.   if (hostsig == SIGSTOP) return TARGET_SIGNAL_STOP;
  951. #endif
  952. #if defined (SIGTSTP)
  953.   if (hostsig == SIGTSTP) return TARGET_SIGNAL_TSTP;
  954. #endif
  955. #if defined (SIGCONT)
  956.   if (hostsig == SIGCONT) return TARGET_SIGNAL_CONT;
  957. #endif
  958. #if defined (SIGTTIN)
  959.   if (hostsig == SIGTTIN) return TARGET_SIGNAL_TTIN;
  960. #endif
  961. #if defined (SIGTTOU)
  962.   if (hostsig == SIGTTOU) return TARGET_SIGNAL_TTOU;
  963. #endif
  964. #if defined (SIGVTALRM)
  965.   if (hostsig == SIGVTALRM) return TARGET_SIGNAL_VTALRM;
  966. #endif
  967. #if defined (SIGPROF)
  968.   if (hostsig == SIGPROF) return TARGET_SIGNAL_PROF;
  969. #endif
  970. #if defined (SIGXCPU)
  971.   if (hostsig == SIGXCPU) return TARGET_SIGNAL_XCPU;
  972. #endif
  973. #if defined (SIGXFSZ)
  974.   if (hostsig == SIGXFSZ) return TARGET_SIGNAL_XFSZ;
  975. #endif
  976. #if defined (SIGWIND)
  977.   if (hostsig == SIGWIND) return TARGET_SIGNAL_WIND;
  978. #endif
  979. #if defined (SIGPHONE)
  980.   if (hostsig == SIGPHONE) return TARGET_SIGNAL_PHONE;
  981. #endif
  982. #if defined (SIGLOST)
  983.   if (hostsig == SIGLOST) return TARGET_SIGNAL_LOST;
  984. #endif
  985. #if defined (SIGWAITING)
  986.   if (hostsig == SIGWAITING) return TARGET_SIGNAL_WAITING;
  987. #endif
  988. #if defined (SIGLWP)
  989.   if (hostsig == SIGLWP) return TARGET_SIGNAL_LWP;
  990. #endif
  991. #if defined (SIGDANGER)
  992.   if (hostsig == SIGDANGER) return TARGET_SIGNAL_DANGER;
  993. #endif
  994. #if defined (SIGGRANT)
  995.   if (hostsig == SIGGRANT) return TARGET_SIGNAL_GRANT;
  996. #endif
  997. #if defined (SIGRETRACT)
  998.   if (hostsig == SIGRETRACT) return TARGET_SIGNAL_RETRACT;
  999. #endif
  1000. #if defined (SIGMSG)
  1001.   if (hostsig == SIGMSG) return TARGET_SIGNAL_MSG;
  1002. #endif
  1003. #if defined (SIGSOUND)
  1004.   if (hostsig == SIGSOUND) return TARGET_SIGNAL_SOUND;
  1005. #endif
  1006. #if defined (SIGSAK)
  1007.   if (hostsig == SIGSAK) return TARGET_SIGNAL_SAK;
  1008. #endif
  1009.   return TARGET_SIGNAL_UNKNOWN;
  1010. }
  1011.  
  1012. int
  1013. target_signal_to_host (oursig)
  1014.      enum target_signal oursig;
  1015. {
  1016.   switch (oursig)
  1017.     {
  1018.     case TARGET_SIGNAL_0: return 0;
  1019.  
  1020. #if defined (SIGHUP)
  1021.     case TARGET_SIGNAL_HUP: return SIGHUP;
  1022. #endif
  1023. #if defined (SIGINT)
  1024.     case TARGET_SIGNAL_INT: return SIGINT;
  1025. #endif
  1026. #if defined (SIGQUIT)
  1027.     case TARGET_SIGNAL_QUIT: return SIGQUIT;
  1028. #endif
  1029. #if defined (SIGILL)
  1030.     case TARGET_SIGNAL_ILL: return SIGILL;
  1031. #endif
  1032. #if defined (SIGTRAP)
  1033.     case TARGET_SIGNAL_TRAP: return SIGTRAP;
  1034. #endif
  1035. #if defined (SIGABRT)
  1036.     case TARGET_SIGNAL_ABRT: return SIGABRT;
  1037. #endif
  1038. #if defined (SIGEMT)
  1039.     case TARGET_SIGNAL_EMT: return SIGEMT;
  1040. #endif
  1041. #if defined (SIGFPE)
  1042.     case TARGET_SIGNAL_FPE: return SIGFPE;
  1043. #endif
  1044. #if defined (SIGKILL)
  1045.     case TARGET_SIGNAL_KILL: return SIGKILL;
  1046. #endif
  1047. #if defined (SIGBUS)
  1048.     case TARGET_SIGNAL_BUS: return SIGBUS;
  1049. #endif
  1050. #if defined (SIGSEGV)
  1051.     case TARGET_SIGNAL_SEGV: return SIGSEGV;
  1052. #endif
  1053. #if defined (SIGSYS)
  1054.     case TARGET_SIGNAL_SYS: return SIGSYS;
  1055. #endif
  1056. #if defined (SIGPIPE)
  1057.     case TARGET_SIGNAL_PIPE: return SIGPIPE;
  1058. #endif
  1059. #if defined (SIGALRM)
  1060.     case TARGET_SIGNAL_ALRM: return SIGALRM;
  1061. #endif
  1062. #if defined (SIGTERM)
  1063.     case TARGET_SIGNAL_TERM: return SIGTERM;
  1064. #endif
  1065. #if defined (SIGUSR1)
  1066.     case TARGET_SIGNAL_USR1: return SIGUSR1;
  1067. #endif
  1068. #if defined (SIGUSR2)
  1069.     case TARGET_SIGNAL_USR2: return SIGUSR2;
  1070. #endif
  1071. #if defined (SIGCHLD) || defined (SIGCLD)
  1072.     case TARGET_SIGNAL_CHLD: 
  1073. #if defined (SIGCHLD)
  1074.       return SIGCHLD;
  1075. #else
  1076.       return SIGCLD;
  1077. #endif
  1078. #endif /* SIGCLD or SIGCHLD */
  1079. #if defined (SIGPWR)
  1080.     case TARGET_SIGNAL_PWR: return SIGPWR;
  1081. #endif
  1082. #if defined (SIGWINCH)
  1083.     case TARGET_SIGNAL_WINCH: return SIGWINCH;
  1084. #endif
  1085. #if defined (SIGURG)
  1086.     case TARGET_SIGNAL_URG: return SIGURG;
  1087. #endif
  1088. #if defined (SIGIO)
  1089.     case TARGET_SIGNAL_IO: return SIGIO;
  1090. #endif
  1091. #if defined (SIGPOLL)
  1092.     case TARGET_SIGNAL_POLL: return SIGPOLL;
  1093. #endif
  1094. #if defined (SIGSTOP)
  1095.     case TARGET_SIGNAL_STOP: return SIGSTOP;
  1096. #endif
  1097. #if defined (SIGTSTP)
  1098.     case TARGET_SIGNAL_TSTP: return SIGTSTP;
  1099. #endif
  1100. #if defined (SIGCONT)
  1101.     case TARGET_SIGNAL_CONT: return SIGCONT;
  1102. #endif
  1103. #if defined (SIGTTIN)
  1104.     case TARGET_SIGNAL_TTIN: return SIGTTIN;
  1105. #endif
  1106. #if defined (SIGTTOU)
  1107.     case TARGET_SIGNAL_TTOU: return SIGTTOU;
  1108. #endif
  1109. #if defined (SIGVTALRM)
  1110.     case TARGET_SIGNAL_VTALRM: return SIGVTALRM;
  1111. #endif
  1112. #if defined (SIGPROF)
  1113.     case TARGET_SIGNAL_PROF: return SIGPROF;
  1114. #endif
  1115. #if defined (SIGXCPU)
  1116.     case TARGET_SIGNAL_XCPU: return SIGXCPU;
  1117. #endif
  1118. #if defined (SIGXFSZ)
  1119.     case TARGET_SIGNAL_XFSZ: return SIGXFSZ;
  1120. #endif
  1121. #if defined (SIGWIND)
  1122.     case TARGET_SIGNAL_WIND: return SIGWIND;
  1123. #endif
  1124. #if defined (SIGPHONE)
  1125.     case TARGET_SIGNAL_PHONE: return SIGPHONE;
  1126. #endif
  1127. #if defined (SIGLOST)
  1128.     case TARGET_SIGNAL_LOST: return SIGLOST;
  1129. #endif
  1130. #if defined (SIGWAITING)
  1131.     case TARGET_SIGNAL_WAITING: return SIGWAITING;
  1132. #endif
  1133. #if defined (SIGLWP)
  1134.     case TARGET_SIGNAL_LWP: return SIGLWP;
  1135. #endif
  1136. #if defined (SIGDANGER)
  1137.     case TARGET_SIGNAL_DANGER: return SIGDANGER;
  1138. #endif
  1139. #if defined (SIGGRANT)
  1140.     case TARGET_SIGNAL_GRANT: return SIGGRANT;
  1141. #endif
  1142. #if defined (SIGRETRACT)
  1143.     case TARGET_SIGNAL_RETRACT: return SIGRETRACT;
  1144. #endif
  1145. #if defined (SIGMSG)
  1146.     case TARGET_SIGNAL_MSG: return SIGMSG;
  1147. #endif
  1148. #if defined (SIGSOUND)
  1149.     case TARGET_SIGNAL_SOUND: return SIGSOUND;
  1150. #endif
  1151. #if defined (SIGSAK)
  1152.     case TARGET_SIGNAL_SAK: return SIGSAK;
  1153. #endif
  1154.     default:
  1155.       /* The user might be trying to do "signal SIGSAK" where this system
  1156.      doesn't have SIGSAK.  */
  1157.       warning ("Signal %s does not exist on this system.\n",
  1158.            target_signal_to_name (oursig));
  1159.       return 0;
  1160.     }
  1161. }
  1162.  
  1163. /* Helper function for child_wait and the Lynx derivatives of child_wait.
  1164.    HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
  1165.    translation of that in OURSTATUS.  */
  1166. void
  1167. store_waitstatus (ourstatus, hoststatus)
  1168.      struct target_waitstatus *ourstatus;
  1169.      int hoststatus;
  1170. {
  1171. #ifdef CHILD_SPECIAL_WAITSTATUS
  1172.   /* CHILD_SPECIAL_WAITSTATUS should return nonzero and set *OURSTATUS
  1173.      if it wants to deal with hoststatus.  */
  1174.   if (CHILD_SPECIAL_WAITSTATUS (ourstatus, hoststatus))
  1175.     return;
  1176. #endif
  1177.  
  1178.   if (WIFEXITED (hoststatus))
  1179.     {
  1180.       ourstatus->kind = TARGET_WAITKIND_EXITED;
  1181.       ourstatus->value.integer = WEXITSTATUS (hoststatus);
  1182.     }
  1183.   else if (!WIFSTOPPED (hoststatus))
  1184.     {
  1185.       ourstatus->kind = TARGET_WAITKIND_SIGNALLED;
  1186.       ourstatus->value.sig = target_signal_from_host (WTERMSIG (hoststatus));
  1187.     }
  1188.   else
  1189.     {
  1190.       ourstatus->kind = TARGET_WAITKIND_STOPPED;
  1191.       ourstatus->value.sig = target_signal_from_host (WSTOPSIG (hoststatus));
  1192.     }
  1193. }
  1194.  
  1195.  
  1196. /* Convert a normal process ID to a string.  Returns the string in a static
  1197.    buffer.  */
  1198.  
  1199. char *
  1200. normal_pid_to_str (pid)
  1201.      int pid;
  1202. {
  1203.   static char buf[30];
  1204.  
  1205.   sprintf (buf, "process %d", pid);
  1206.  
  1207.   return buf;
  1208. }
  1209.  
  1210. static char targ_desc[] = 
  1211.     "Names of targets and files being debugged.\n\
  1212. Shows the entire stack of targets currently in use (including the exec-file,\n\
  1213. core-file, and process, if any), as well as the symbol file name.";
  1214.  
  1215. void
  1216. _initialize_targets ()
  1217. {
  1218.   current_target = &dummy_target;
  1219.   cleanup_target (current_target);
  1220.  
  1221.   add_info ("target", target_info, targ_desc);
  1222.   add_info ("files", target_info, targ_desc);
  1223.  
  1224.   if (!STREQ (signals[TARGET_SIGNAL_LAST].string, "TARGET_SIGNAL_MAGIC"))
  1225.     abort ();
  1226. }
  1227.